package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.util.AppUtil;
import com.ccp.dev.form.service.impl.ScriptImpl;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WQbdFalProblemresetDao;
import com.ccp.dev.qbdms.model.WQbdReportTemplate;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProcessingTimeUtil;
import com.ccp.dev.system.model.Dictionary;
import com.ccp.dev.system.service.DictionaryService;
import com.ccp.dev.system.service.SysOrgService;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by zzy on 2020/7/20.
 *
 * @Author zzy
 * @Date 2020/7/20
 * @describe 售后质量问题统计service
 */
@Service
public class AfterSaleQualityProbleService extends BaseService<WQbdReportTemplate> {
    @Resource
    private WQbdFalProblemresetDao wQbdFalProblemresetDao;
    @Resource
    private SysOrgService sysOrgService;
    @Resource
    private ScriptImpl scriptimpl;

    /**
     * 获取统计图数据,只有一按年统计
     * @param start 开始日期
     * @param end 结束日期
     * @return
     */
    public Map<String, Object> getChartData(Date start, Date end) {
        Map<String, Object> resultmap = Maps.newHashMap();
        resultmap.put("bardata", getBarChartData(start, end));
        resultmap.put("piedata", getPieChartData(start, end));
        return resultmap;
    }

    /**
     * 获取柱图数据
     * @param start
     * @param end
     * @return
     */
    private Map<String, Object> getBarChartData(Date start, Date end) {
        List<Map<String, Object>> querydate = ProcessingTimeUtil.packagingTime("andtj", start, end);
        String titlestr = ProcessingTimeUtil.converDateToTitleStr("andtj", start, end);
        Map<String, Object> resultmap = new LinkedHashMap<>(16);
        List<String> titlelist = new ArrayList<>(16);
        List<Integer> datalist = new ArrayList<>(16);
        int secretValue=0;
        for (Map<String, Object> querymap : querydate) {
            querymap.put("belongdeptname", BaseUtil.getCurrentTenOrgCodePath() + "%");
            querymap.put("secret", scriptimpl.getCurrentUser().getPrivacyLevel());
            ZhiLiangConst.addReportCodeConditions(querymap);
            Map<String, Object> temp = wQbdFalProblemresetDao.getAfterSaleQualityBarDataByDate(querymap);
            secretValue=getSecretValue(temp, secretValue);
            int nums = 0;
            if (null != temp && null != temp.get("NUMS")) {
                nums = Integer.valueOf(temp.get("NUMS").toString());
            }
            datalist.add(nums);
            titlelist.add(querymap.get("title").toString());
        }
        resultmap.put("xtitlelist", titlelist);
        resultmap.put("datalist", datalist);

        String secretStr = getSecret(String.valueOf(secretValue));
        resultmap.put("title", titlestr + "售后质量问题条形图"+secretStr);
        return resultmap;
    }

    /**
     * 获取饼图数据
     * @param start 开始日期
     * @param end 结束日期
     * @return
     */
    private Map<String, Object> getPieChartData(Date start, Date end) {
        Map<String, Object> querymap = ProcessingTimeUtil.converSimpleDateRange("andtj", start, end);
        querymap.put("belongdeptname", BaseUtil.getCurrentTenOrgCodePath() + "%");
        querymap.put("secret", scriptimpl.getCurrentUser().getPrivacyLevel());
        ZhiLiangConst.addReportCodeConditions(querymap);
        String titlestr = ProcessingTimeUtil.converDateToTitleStr("andtj", start,end);
        Map<String, Object> resultmap = new LinkedHashMap<>();
        // 饼图中的时间需要处理
        List<Map<String, Object>> datalist = wQbdFalProblemresetDao.getAfterSaleQualityPieDataByDate(querymap);
        if (null == datalist || datalist.size() < 1) {
            return resultmap;
        }
        List<String> titlelist = new ArrayList<>();
        int secretValue=0;
        for (Map<String, Object> stringObjectMap : datalist) {
            titlelist.add(String.valueOf(stringObjectMap.get("name")));
            secretValue=getSecretValue(stringObjectMap, secretValue);
        }
        resultmap.put("xtitlelist", titlelist);
        resultmap.put("datalist", datalist);
        String secretStr = getSecret(String.valueOf(secretValue));
        resultmap.put("title", titlestr + "售后质量问题饼图"+secretStr);
        return resultmap;
    }

    private int getSecretValue(Map dataMap, int secretValue) {
        if (null != dataMap.get("SECRET")
                && Integer.parseInt((String) dataMap.get("SECRET")) > secretValue) {
            secretValue = Integer.parseInt((String) dataMap.get("SECRET"));
        }
        return secretValue;
    }

    private String getSecret(String secretValue) {
        DictionaryService dictionaryService = AppUtil.getBean(DictionaryService.class);
        List<com.ccp.dev.system.model.Dictionary> dicList = dictionaryService.getDicByNodeKey("mj", false);
        String secret = "";
        for (Dictionary dictionary : dicList) {
            if (dictionary.getItemValue().equals(secretValue)) {
                // 密级显示名称
                secret = dictionary.getItemName();
                break;
            }
        }
        return secret;
    }

}
